<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

use App\Providers\VinServiceProvider;

class CarOil extends Model
{

    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'car_oil';

    /**
     * 根据对应规则获取客户新油品（相对于python抓取到的油品）
     */
    public static function getOilByRule( $mi_key, $old_oil_name='' ){

        $res = [];
        $oil_exchange_special_sql = 'select o.* from car_oil_exchange_special oexs 
                    inner join car_oil o on oexs.oil_id=o.id 
                    where mi_key="' . $mi_key . '"';
        $oil = DB::select( $oil_exchange_special_sql );

        if( empty( $oil ) ){
            $oil_exchange_sql = 'select o.* from car_oil_exchange oex 
                                  inner join car_oil o on oex.oil_id=o.id 
                                  where oex.old_oil_name = "' . $old_oil_name . '"';
            $oil = DB::select( $oil_exchange_sql );
        }

        if( !empty( $oil ) ){
            $res = [
                'id'=>$oil[0]->id,
                'name'=>$oil[0]->name,
                'img_url'=>$oil[0]->img_url,
                'link'=>$oil[0]->link,
            ];
        }
        return $res;
    }

    public static function edit( $id, $data ){
//        $car_oil = self::where( 'id', $id )->first();
        if( $id != 0){
            $car_oil = self::find( $id );
        }else{
            $car_oil = new CarOil();
        }



        if( !empty($_FILES['oil_img']) && is_uploaded_file($_FILES['oil_img']['tmp_name']) ){
            $res = self::uploadImage( $_FILES['oil_img'] );
            if( empty( $res['status'] ) ){
                $car_oil->img_url = $res['img_url'];
            }else{
                return $res;
            }
        }
        $car_oil->name = $data['name'];
        $car_oil->link = $data['link'];
        return $car_oil->save();
    }

    public static function uploadImage( $imgFile ){


        $res = [
            'status'=>1
        ];
        $arrType=array('image/jpg','image/gif','image/png','image/bmp','image/pjpeg');
        $max_size='500000';      // 最大文件限制（单位：byte）

        $file = $imgFile;

        if(!is_uploaded_file($file['tmp_name'])){ //判断上传文件是否存在
            $res['description'] = '文件不存在';
        }else if($file['size']>$max_size){  //判断文件大小是否大于500000字节
            $res['description'] = '上传文件太大,不能超过500K';
        }else if(!in_array($file['type'],$arrType)){  //判断图片文件的格式
            $res['description'] = '上传文件格式不对';
        }else{

//                $imageSize=getimagesize($file['tmp_name']);

            $fname=$file['name'];
            $ftype=explode('.',$fname);
            $picName = 'gearbox_oil/' . time() . '.' . $ftype[1];
            $picName_all = 'frontend/images/' . $picName;

            if( !move_uploaded_file( $file['tmp_name'], $picName_all ) ){

                $res['description'] = '移动文件出错';

            }
            else{
                $res['status'] = 0;
                $res['img_url'] = $picName;
            }
        }

        return $res ;
    }
}